﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Collections;
using System.IO;

namespace FtpUpload
{
    public class UnUploadFileInfo
    {
        /// <summary>
        /// 未上传成功的文件路径
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 已经上传的文件大小
        /// </summary>
        public long UploadSize { get; set; }
    }

    public class LocationManager
    {
        static string m_file_name = Application.StartupPath + "\\lf.config";

        /// <summary>
        /// 读取未完全上传的文件
        /// </summary>
        /// <returns></returns>
        public static List<UnUploadFileInfo> GetUnloadFile()
        {
            List<UnUploadFileInfo> t_list = null;
            try
            {
                DataSet ds = new DataSet();
         
                if (File.Exists(m_file_name))
                {
                    UnUploadFileInfo t_file_info;
                    ds.ReadXml(m_file_name);
                    if (ds.Tables.Count > 0)
                    {
                        t_list = new List<UnUploadFileInfo>();
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            t_file_info = new UnUploadFileInfo();
                            t_file_info.FileName = dr["FileName"].ToString();
                            t_file_info.UploadSize = dr["UploadSize"]==null?0:Convert.ToInt64(dr["UploadSize"].ToString());
                            t_list.Add(t_file_info);
                        }
                    }
                }

                return t_list;
            }
            catch
            {
                return null;
            }
           
        }

        #region 保存当前数据源设置

        public static DataTable CreateDataTable()
        {
            DataTable t_table = new DataTable("FileConfig");
            DataColumn t_col;
            
            t_col = new DataColumn();
            t_col.DataType = Type.GetType("System.String");
            t_col.ColumnName = "FileName";
            t_table.Columns.Add(t_col);

            t_col = new DataColumn();
            t_col.DataType = Type.GetType("System.Int64");
            t_col.ColumnName = "UploadSize";
            t_table.Columns.Add(t_col);

           

            return t_table;
        }

        /// <summary>
        /// 增加当前数据源设置
        /// </summary>
        /// <param name="data"></param>
        public static bool AddSettings(UnUploadFileInfo i_file)
        {
            try
            {
                DataSet ds = new DataSet();
                if (!File.Exists(m_file_name))
                {
                    #region 第一次添加
                    DataTable dt = CreateDataTable();
                    DataRow t_rown = dt.NewRow();
                    t_rown["FileName"] = i_file.FileName;
                    t_rown["UploadSize"] = i_file.UploadSize;

                    ds.Tables.Add(dt);
                    #endregion
                }
                else
                {
                    #region 追加

                    ds.ReadXml(m_file_name);
                    if (ds.Tables.Count > 0)
                    {
                        DataRow[] t_drs = ds.Tables[0].Select("FileName='" + i_file.FileName + "'");
                        if (t_drs.Length > 0)
                        {
                            long t_upload_size = 0;
                            if (!long.TryParse(t_drs[0]["UploadSize"].ToString(), out t_upload_size))
                                t_upload_size = 0;
                            if (i_file.UploadSize > t_upload_size)
                                t_upload_size = i_file.UploadSize;
                            t_drs[0]["UploadSize"] = t_upload_size;
                           
                        }
                        else
                        {
                            DataRow t_rown = ds.Tables[0].NewRow();
                            t_rown["FileName"] = i_file.FileName;
                            t_rown["UploadSize"] = i_file.UploadSize;
                            ds.Tables[0].Rows.Add(t_rown);

                        }
                    }
                    else
                    {
                        DataTable dt = CreateDataTable();
                        DataRow rown = dt.NewRow();
                        rown["FileName"] = i_file.FileName;
                        rown["UploadSize"] = i_file.UploadSize;
                        dt.Rows.Add(rown);

                        ds.Tables.Add(dt);
                    }
                    #endregion

                }
                ds.WriteXml(m_file_name);
                return true;
            }
            catch
            {
               
                return false;
            }
        }

        /// <summary>
        /// 删除指定数据源的配置信息
        /// </summary>
        /// <returns></returns>
        public static bool DelSetting(string i_file_name)
        {
            try
            {
                DataSet ds = new DataSet();
                if (File.Exists(m_file_name))
                {
                    ds.ReadXml(m_file_name);
                    if (ds.Tables.Count > 0)
                    {
                        string strwhere = "FileName='" + i_file_name + "' ";
                       
                        DataRow[] t_drs = ds.Tables[0].Select(strwhere);
                        if (t_drs.Length > 0)
                        {
                            ds.Tables[0].Rows.Remove(t_drs[0]);
                        }
                        ds.Tables[0].AcceptChanges();
                    }
                }
                ds.WriteXml(m_file_name);
                return true;
            }
            catch
            {
                return false;
            }
        }

       


        #endregion
    }
}
